package cl.blueprintsit.filmdisk.model;

import cl.blueprintsit.filmdisk.utils.ActorsManager;
import cl.blueprintsit.filmdisk.utils.DirectoryFilmParser;
import cl.blueprintsit.utils.files.FileUtils;
import cl.blueprintsit.utils.files.NotADirectoryException;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * This class is responsible for inspecting a directory and analyse the films inside.
 */
public class DiskScanner {

    private final File baseDirectory;

    public DiskScanner(String baseDirPath) throws Exception {
        File aDirectory = new File(baseDirPath);
        if (!aDirectory.exists()) {
            throw new Exception(baseDirPath + " does not exists.");
        }

        if (!aDirectory.isDirectory()) {
            throw new Exception(baseDirPath + " is not a directory");
        }

        this.baseDirectory = aDirectory;
    }

    public static void main(String[] args) throws Exception {

        String baseDir = (args.length == 0 || (args[0] == null)) ? "x:\\films" : args[0];

        DiskScanner diskScanner = new DiskScanner(baseDir);
        System.out.println("Directory Films found: " + diskScanner.getDirectoryFilms().size());
        List<Actor> actors = diskScanner.getActors();
        System.out.println("Actors found: " + actors.size());
        Collections.sort(actors);

        System.out.println(actors);
    }

    private List<Actor> getActors() {

        for (Film directoryFilm : parseFilms()) {
            List<Actor> actors = directoryFilm.getActors();
            actors = ActorsManager.getInstance().normalizeActors(actors);

        }

        return ActorsManager.getInstance().getActors();
    }

    /**
     * This method is responsible for retrieving all the folders from the base folder, recursively, and parse them in
     * order to obtain Films and extract their data.
     *
     * @return A list of Films, parsed from the base directory.
     */
    private List<Film> parseFilms() {
        ArrayList<Film> films = new ArrayList<Film>();

        /* For each directory film, the folder is parsed */
        DirectoryFilmParser parser = new DirectoryFilmParser();
        for (File directoryFilm : getDirectoryFilms()) {
            Film film = parser.parseFilm(directoryFilm);
            films.add(film);
        }

        return films;
    }

    private List<File> getDirectoryFilms() {
        List<File> directoryFilms;
        try {
            directoryFilms = FileUtils.getDirectoryFromFolder(this.baseDirectory);

        } catch (FileNotFoundException e) {
            // TODO: Comment this impossible case
            return Collections.emptyList();
        } catch (NotADirectoryException e) {
            // TODO: Comment this impossible case
            return Collections.emptyList();
        }

        return directoryFilms;
    }


}
